/*
 * @Description: 上传组件
 * @Author: paddies
 * @Date: 2023-12-06 20:20:10
 * @LastEditors: paddies
 * @LastEditTime: 2024-11-18 11:59:04
 * @FilePath: /luntai-user-app/Users/paddies/Desktop/code/A-team/luntai/shared_components/lib/build-upload/buildUpload.dart
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_components/build-bottom-sheet/buildBottomSheetWidget.dart';
import 'package:shared_components/build-choose-image/buildChooseImage.dart';
import 'package:shared_components/build-image/buildImage.dart';

Widget buildUpload(
    {List list = const [],
    int rowCount = 2,
    int max = 99,
    String imageType = '',
    bool disabled = false,
    String type = 'edit',
    Widget? slotWidget,
    Function? onOcrContent,
    Function? onChange}) {
  return Container(
      width: double.infinity,
      child: Wrap(
          spacing: 0, //主轴上子控件的间距
          runSpacing: 15,
          children: [
            ...list.asMap().keys.map((index) {
              return FractionallySizedBox(
                  widthFactor: 1 / rowCount,
                  child: Padding(
                      padding: const EdgeInsets.only(left: 5, right: 5),
                      child: Container(
                          width: double.infinity,
                          height: 100,
                          child: GestureDetector(
                            onLongPress: () {
                              if (disabled) {
                                return;
                              }
                              if (type != 'edit') {
                                return;
                              }
                              print("长按事件");
                              showModalBottomSheet(
                                  builder: (BuildContext context) {
                                    //构建弹框中的内容
                                    return buildBottomSheetWidget(
                                        itemList: [
                                          {
                                            "label": "修改",
                                            "onClick": () {
                                              chooseImage(
                                                  type: "All",
                                                  multiple: false,
                                                  imageType: imageType,
                                                  onOcrContent: (orcData) {
                                                    if (onOcrContent != null) {
                                                      onOcrContent(orcData);
                                                    }
                                                  },
                                                  onSuccess: (imgUrl) {
                                                    if (onChange != null) {
                                                      onChange('change', imgUrl,
                                                          index);
                                                    }
                                                  });
                                            }
                                          },
                                          {
                                            "label": "删除",
                                            "onClick": () {
                                              if (onChange != null) {
                                                onChange('del', '', index);
                                              }
                                            }
                                          }
                                        ],
                                        onSelect: (code) {
                                          if (code == "Camera") {}
                                          if (code == "Gallery") {}
                                        });
                                  },
                                  backgroundColor: Colors.transparent, //重要
                                  context: Get.overlayContext!);
                            },
                            child: buildImage(
                                url: list[index],
                                provider: true,
                                type: 'ClipRect',
                                fit: BoxFit.scaleDown),
                          ))));
            }).toList(),
            Visibility(
                visible: type == 'edit' && list.length < max && !disabled,
                child: GestureDetector(
                    onTap: () {
                      chooseImage(
                          type: "All",
                          multiple: true,
                          imageType: imageType,
                          onOcrContent: (orcData) {
                            if (onOcrContent != null) {
                              onOcrContent(orcData);
                            }
                          },
                          onSuccess: (imgUrl) {
                            if (onChange != null) {
                              if (list.length < max) {
                                onChange('add', imgUrl, '');
                              }
                            }
                          });
                    },
                    child: FractionallySizedBox(
                        widthFactor: 1 / rowCount,
                        child: slotWidget ??
                            Padding(
                              padding: const EdgeInsets.only(left: 5, right: 5),
                              child: Container(
                                  width: double.infinity,
                                  height: 100,
                                  decoration: const BoxDecoration(
                                    color: Colors.transparent,
                                    // color: Color(0xff666666),
                                    borderRadius: BorderRadius.all(
                                        Radius.circular(5.0)), //圆角
                                  ),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Container(
                                          width: 100,
                                          height: 100,
                                          color: Colors.transparent,
                                          child: Stack(
                                            alignment: Alignment.center,
                                            children: [
                                              Column(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.center,
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                children: [
                                                  Icon(
                                                    Icons.add,
                                                    size: 20,
                                                    color: Color(0xff999999),
                                                  ),
                                                  Text(
                                                    "上传您的",
                                                    style: TextStyle(
                                                        fontSize: 12,
                                                        color:
                                                            Color(0xff999999)),
                                                  ),
                                                  Text(
                                                    "照片",
                                                    style: TextStyle(
                                                        fontSize: 12,
                                                        color:
                                                            Color(0xff999999)),
                                                  )
                                                ],
                                              ),
                                              Align(
                                                alignment: Alignment.topLeft,
                                                child: DashedLine(
                                                  dashedWidth: 4,
                                                  count: 9,
                                                  dashedHeight: 2,
                                                  axis: Axis.horizontal,
                                                ),
                                              ),
                                              Align(
                                                alignment: Alignment.bottomLeft,
                                                child: DashedLine(
                                                  dashedWidth: 4,
                                                  count: 9,
                                                  dashedHeight: 2,
                                                  axis: Axis.horizontal,
                                                ),
                                              ),
                                              Align(
                                                alignment: Alignment.centerLeft,
                                                child: DashedLine(
                                                  dashedWidth: 2,
                                                  count: 9,
                                                  dashedHeight: 4,
                                                  axis: Axis.vertical,
                                                ),
                                              ),
                                              Align(
                                                alignment:
                                                    Alignment.centerRight,
                                                child: DashedLine(
                                                  dashedWidth: 2,
                                                  count: 9,
                                                  dashedHeight: 4,
                                                  axis: Axis.vertical,
                                                ),
                                              )
                                            ],
                                          ))
                                      // Container(
                                      //   width: 100,
                                      //   height: 100,
                                      //   decoration: BoxDecoration(
                                      //     color: Colors.red,
                                      //     border: Border.all(
                                      //       color: Colors.grey, // 虚线颜色
                                      //       style: BorderStyle.solid, // 虚线样式
                                      //     ),
                                      //   ),
                                      // )
                                    ],
                                  )

                                  //  const Icon(
                                  //   Icons.add,
                                  //   color: Colors.white,
                                  //   size: 30,
                                  // ),
                                  ),
                            )))),
          ]));
}

class DashedLine extends StatelessWidget {
  final Axis axis;
  final double dashedWidth;
  final double dashedHeight;
  final int count;
  final Color color;

  DashedLine(
      {required this.axis,
      required this.dashedWidth,
      required this.dashedHeight,
      this.color = const Color(0xff999999),
      this.count = 10});

  @override
  Widget build(BuildContext context) {
    return Flex(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      direction: axis,
      children: List.generate(count, (_) {
        return SizedBox(
          width: dashedWidth,
          height: dashedHeight,
          child: DecoratedBox(
              decoration: BoxDecoration(
            color: color,
          )),
        );
      }),
    );
  }
}
